package com.example.Lezedata.ui.activity.person;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.activity.setting.PinSettingVc;
import com.example.Lezedata.ui.fragment.commonfragment.AlertFragment;
import com.example.Lezedata.ui.fragment.commonfragment.TransactionPinView;
import com.example.Lezedata.util.GBToast;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;
import common.Common;
import common.Errorcode;

public class DepositVc extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_deposit;
    }

    @Override
    public CharSequence title() {
        return "Deposit";
    }

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findView();
    }

    private EditText editText;

    void findView(){
        editText = findViewByTag("input");
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView)v;
                editText.setText(textView.getText());
            }
        };
        findViewByTag("M10").setOnClickListener(listener);
        findViewByTag("M20").setOnClickListener(listener);
        findViewByTag("M50").setOnClickListener(listener);
        findViewByTag("M100").setOnClickListener(listener);
        findViewByTag("M200").setOnClickListener(listener);
        findViewByTag("M300").setOnClickListener(listener);
        findViewByTag("M400").setOnClickListener(listener);
        findViewByTag("M500").setOnClickListener(listener);
        findViewByTag("pay").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay();
            }
        });
    }

    void setPinPsd(){
        AlertFragment fragment = new AlertFragment();
        fragment.text = "Please set your PIN";
        fragment.okListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(PinSettingVc.class);
            }
        };
        fragment.show(getSupportFragmentManager(),"alert");
    }

    void pay(){
        if (editText.getText().length() == 0){
            GBToast.shortToast("The withdrawal amount cannot be empty");
            return;
        }
        boolean hasPinPsd = GBUserManager.instance().hasPIN;
        if (!hasPinPsd){
            setPinPsd();
            return;
        }
        final TransactionPinView ms = TransactionPinView.newInstance();
        ms.inputListener = new TransactionPinView.InputListener() {
            @Override
            public void InputComplete(String text) {
                ms.dismiss();
                payForPsd(text);
            }
        };
        ms.show(getSupportFragmentManager(),"TransactionPinView");
    }

    void payForPsd(String pwd){
        Integer num = Integer.parseInt(editText.getEditableText().toString());
        GBNetRequest.stubUser()
                .recharge(Bid.RechargeRequest.newBuilder().
                        setAmount(num).
                        setTransPw(GBUtil.getSHA256(pwd)).
                        build(),ServerResponseObserver.observer(new ServerResponseObserver.requestListener< Common.CommonResponse>() {
                    @Override
                    public void complete(Common.CommonResponse value, GBError t) {
                        if (t == null){
                            Intent intent = new Intent(mContext,TransitionPage.class);
                            if (value.getCode() == Errorcode.TipCode.Code100000.getNumber()){
                                intent.putExtra(TransitionPage.Key_title,"Successful recharge");
                                intent.putExtra(TransitionPage.Key_image,R.drawable.deposit__success);
                            }else {
                                intent.putExtra(TransitionPage.Key_title,"Payment failed！");
                                intent.putExtra(TransitionPage.Key_image,R.drawable.deposit__failure);
                            }
                            jumpActivityIntent(intent);
                        }
                    }
                }));
    }
}